// sis puella oier
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
ll read(){
    ll xx = 0, f = 1; char ch = getchar();
    for (;!isdigit(ch); ch = getchar())f = (ch == '-' ? -1 : 1);
    for (; isdigit(ch); ch = getchar())xx = xx * 10 + ch - '0';
    return xx * f;
}
const int N = 270010;
int n, m, aa[N], c[N], V, lg[N], a[N];
int de[N << 2];
int getlg(int x){
    if ((1ll << lg[x]) == x)return lg[x];
    return lg[x] + 1;
}
int ans[N];
struct node{
    int idw, sumb;
}tr[N << 2];
node mg(node x, node y, int op, int k){
    if (x.sumb == 0 && y.sumb == 0){
        if ((op == 0 && a[x.idw] >= k) || (op == 1 && a[y.idw] < k))return x;
        else return y;
    }
    else if (x.idw == 0 && y.idw == 0){
        return node{0, x.sumb + y.sumb};
    }
    else {
        if (x.sumb == 0){
            if (op == 0){
                if (a[x.idw] >= k)return x;
                else return y;
            }
            else {
                if (y.idw == 0)return node{x.idw, y.sumb};
                node ret = node{0, y.sumb};
                if (a[y.idw] >= k)ret.idw = y.idw;
                else ret.idw = x.idw;
                return ret;
            }
        }
        else if (y.idw == 0){
            if (op == 0){
                node ret = node{0, x.sumb + y.sumb};
                if (a[x.idw] >= k)ret.idw = x.idw;
                return ret;
            }
            else {
                return node{x.idw, x.sumb + y.sumb};
            }
        }
        else assert(0);
    }
}
void build(int x, int l, int r, int k){
    if (l == r){
        if (l <= n)tr[x] = node{l, 0};
        else tr[x] = node{0, l};
        return ;
    }
    int mid = (l + r) >> 1; build(x << 1, l, mid, k - 1), build(x << 1 | 1, mid + 1, r, k - 1);
    tr[x] = mg(tr[x << 1], tr[x << 1 | 1], de[x], k);
}
node get(int l, int r){
    return node{0, (l + r) * (r - l + 1) / 2};
}
node query(int x, int l, int r, int nr, int k, bool op){
    if (r <= nr)return tr[x];
    int mid = (l + r) >> 1;
    if (nr <= mid){
        if (!op)return query(x << 1, l, mid, nr, k - 1, op);
        else return mg(query(x << 1, l, mid, nr, k - 1, op), get(mid + 1, r), de[x], k);
    }
    // if (!op)return mg(query(x << 1, l, mid, nr, k - 1, op), query(x << 1 | 1, mid + 1, r, nr, k - 1, true), de[x], k);
    // else 
    return mg(query(x << 1, l, mid, nr, k - 1, op), query(x << 1 | 1, mid + 1, r, nr, k - 1, true), de[x], k);
}
void solve(){
    int X[4];  for (int i = 0; i < 4; ++i)X[i] = read();
    for (int i = 1; i <= n; ++i)a[i] = aa[i] ^ X[i % 4];
    // for (int i = 1; i <= n; ++i)cout<<a[i]<<" ";cout<<endl;
    for (int i = 1; i <= m; ++i)ans[i] = 0; int tot = (1ll << (V));
    // cout<<tot<<endl;
    build(1, 1, tot, V);
    // cout<<tr[1].idw+tr[1].sumb<<endl;
    for (int i = 1; i <= m; ++i){
        node it = query(1, 1, tot, c[i], V, false);
        ans[i] = it.idw + it.sumb;
        // assert(c[i] <= n);
        // cout<<c[i]<<" : "<<ans[i]<<endl;
    }
    ans[0] = 0;
    for (int i = 1; i <= m; ++i)ans[0] = ans[0] ^ (i * ans[i]);
    printf("%lld\n", ans[0]);
}
char ch[N];
signed main(){
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    n = read(), m = read();
    for (int i = 1; i <= n; ++i)aa[i] = read();
    lg[0] = -1; for (int i = 1; i <= n; ++i)lg[i] = lg[i >> 1] + 1;
    V = getlg(n);
    // cout<<V<<" "<<n<<" "<<(1ll << V)<<endl;
    for (int i = 1; i <= m; ++i)c[i] = read();//, cout<<c[i]<<" "<<getlg(c[i])<<endl;
    for (int i = 1; i <= V; ++i){
        scanf("%s", ch + 1); int len = strlen(ch + 1), ad = (1ll << (V - i)) - 1;
        for (int j = 1; j <= len; ++j)de[ad + j] = ch[j] - '0';
    }
    // for (int i = 1; i <= 7; ++i)cout<<de[i]<<" ";cout<<endl;
    // cout<<V<<endl;
    int _ = read();
    while(_--)solve();
    return 0;
}

